﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using TencentCloud.Bda.V20200324.Models;
using ZenSystemService.Api.Business;
using ZenSystemService.Api.Common.Auth;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.Entities;
using ZenSystemService.Api.Services;
using ZenSystemService.Api.Common;
using ZenSystemService.Api.Common.Encryption;
namespace ZenSystemService.Api.Controllers
{
    [Route("api/common")]
    [ApiController]
    public class CommonController: ControllerBase
    {
        private readonly SMSVerificationCodeService _smsVerificationCodeService;

        private readonly AreaService _areaService;

        private readonly CourseService _courseService;

        private readonly UserBusiness _userBusiness;

        private readonly ZenBrightBusiness _zenBrightBusiness;

        private readonly IEncryptionService _encryptionService;

        public CommonController(SMSVerificationCodeService smsVerificationCodeService, AreaService areaService
            , CourseService courseService, UserBusiness userBusiness, ZenBrightBusiness zenBrightBusiness, IEncryptionService encryptionService)
        {
            _smsVerificationCodeService = smsVerificationCodeService;
            _areaService = areaService;
            _courseService = courseService;
            _userBusiness = userBusiness;
            _zenBrightBusiness = zenBrightBusiness;
            _encryptionService= encryptionService;
        }

        [HttpGet("Test")]
        public async Task<string> Test()
        {
            /*
            TraineeCreateSubAccountRequest _traineeCreateSubAccountRequest = new TraineeCreateSubAccountRequest();

            _traineeCreateSubAccountRequest.RealName = "张三";
            _traineeCreateSubAccountRequest.Birthday= DateTime.Now;
            _traineeCreateSubAccountRequest.Height = 180;
            _userBusiness.CreateSubAccount(_traineeCreateSubAccountRequest);
            */

            /*
            ZenBrightCreateUserInfoRequest _zenBrightCreateUserInfoRequest=new ZenBrightCreateUserInfoRequest();
            _zenBrightCreateUserInfoRequest.zenSystemUserId = 118;
            _zenBrightCreateUserInfoRequest.userName= "1233333TTTTT3";
            _zenBrightCreateUserInfoRequest.password= "0000000000000000";
            _zenBrightCreateUserInfoRequest.realName= "张三11";
            _zenBrightCreateUserInfoRequest.gender = 0;
            _zenBrightCreateUserInfoRequest.mobile = "13011111222";
            _zenBrightCreateUserInfoRequest.birthday = "2021-2-1";
            _zenBrightCreateUserInfoRequest.remark = "";
            _userBusiness.UpdateZenBrightUserInfo(_zenBrightCreateUserInfoRequest);
            */
            //_zenBrightBusiness.GetTestList();
            //_zenBrightBusiness.SendDefaultTestOrder(5406);

            string str=_encryptionService.Decrypt("cCQyu9DavMtkLuZq8xvgpA==");

            return str;
        }



        /// <summary>
        /// 获取 区域信息列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetAreaList")]
        public async Task<List<AreaDTO>> GetAreaList()
        {
            List<AreaDTO> AreaDTOList = new List<AreaDTO>();
            IEnumerable<Area> list = await _areaService.GetAllAreaAsync();
            List<Area> AreaList = list.ToList();
            List<Area> AreaList1 = list.Where(item => item.ParentID == 0).ToList();
            for (int i = 0; i < AreaList1.Count; i++)
            {
                AreaDTO _areaDTO = new AreaDTO();
                _areaDTO.code = AreaList1[i].Code.ToString();
                _areaDTO.value = AreaList1[i].ID.ToString();
                _areaDTO.label = AreaList1[i].Name;
                BaseBusiness.Instance.LoadSubAreaDTO(AreaList, AreaList1[i].ID, 2, ref _areaDTO);
                AreaDTOList.Add(_areaDTO);
            }
            return AreaDTOList;
        }

        /// <summary>
        /// 获取学员关系信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetTraineeRelationList")]
        public async Task<List<DicObj>> GetTraineeRelationList()
        {
            List<DicObj> list = new List<DicObj>();
            foreach (TraineeRelation item in Enum.GetValues(typeof(TraineeRelation)))
            {
                DicObj _dicObj = new DicObj();
                _dicObj.name = item.GetDesc();
                _dicObj.value = (int)item;
                list.Add(_dicObj);
            }
            return list;
        }


        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("SendVerificationCodeSMS")]
        public async Task<ReturnObjectT<SendSMSResponse>> SendVerificationCodeSMS(ReceiveObjectT<SendSMSRequest> _receiveObjectT)
        {
            ReturnObjectT<SendSMSResponse> _returnObject = new ReturnObjectT<SendSMSResponse>();

            string msg;            
            string phone = _receiveObjectT.Data.Phone;

            Random random = new Random();
            string randomNum = random.Next(100000, 999999).ToString();

            if (!TencentSmsBusiness.Instance.SendVerificationCodeSMS(phone, randomNum, out msg))
            {
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = msg;
                return _returnObject;
            }
            SMSVerificationCode _smsVerificationCode = new SMSVerificationCode();
            _smsVerificationCode.Phone = phone;
            _smsVerificationCode.VerificationCode = randomNum;
            _smsVerificationCode.SendTime = System.DateTime.Now;

            bool isSuccess = await _smsVerificationCodeService.AddSMSVerificationCodeAsync(_smsVerificationCode);
            if (isSuccess)
            {
                _returnObject.Code = 1;
                SendSMSResponse _sendSMSResponse = new SendSMSResponse();
                _sendSMSResponse.VCode = randomNum;
                _returnObject.Data = _sendSMSResponse;
            }
            else
            {
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = "保存发送验证码信息失败";
            }
            return _returnObject;

        }



    }
}
